<template>
  <div class="login-container">
    <el-card class="el-card">
      <img class="logo" src="../assets/logo.fefe7e03.png" />
      <!-- 表单 -->
      <el-form  class="el-form" :model="form" :rules="rules" ref="form">
        <el-form-item prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input v-model="form.code" placeholder="请输入验证码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-checkbox :value="true">我已阅读并同意「用户协议」和「隐私条款」</el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" class="">登录</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { loginAPI } from '@/api/index.js'
import { setItem } from '@/utils/storage.js'
export default {
  name: 'loginIndex',
  data () {
    return {
      form: {
        mobile: '',
        code: ''
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { len: 6, message: '验证码6字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    onSubmit () {
      this.$refs.form.validate(async valid => {
        if (valid) {
          try {
            const { data: res } = await loginAPI({
              mobile: this.form.mobile,
              code: this.form.code
            })
            // 将token存储在本地
            setItem('geek-client-pc-store', res.data.token)
            // 跳转到主页面
            this.$router.push('/layout')
            console.log(res)
          } catch (error) {
            console.log(error)
          }
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.login-container {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: url(../assets/login.1e94db24.png);
 .el-card {
  display: flex;
  justify-content: center;
    width: 440px;
    height: 380px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 0 50px rgba(0, 0, 0, 0.1);
    .logo {
      width: 200px;
      display: block;
      margin-left: 80px;
    }
    .el-form{
      padding: 10px 20px 0;
      el-form-item{
        margin-bottom: 10px;
      }
        .el-button {
        width: 100%;
        height: 40px;
      }
    }
  }
}
</style>
